﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace PhoneApp1
{
    public partial class Poll1 : PhoneApplicationPage
    {
        public Poll1()
        {
            InitializeComponent();
        }

        Poll p;

        private void DisplayMessage(string message, string caption)
        {
            this.Dispatcher.BeginInvoke(
                    delegate
                    {
                        MessageBox.Show(message, caption, MessageBoxButton.OK);
                    });
        }

        protected override void OnNavigatedTo(NavigationEventArgs nea)
        {
            base.OnNavigatedTo(nea);

            String arg = NavigationContext.QueryString["guid"];
            if (arg == "")
            {
                DisplayMessage("Umm... Not sure how you got here... i have nothing to show you...", "Error");
                NavigationService.GoBack();
            }
            Guid g = new Guid(arg);
            foreach (Poll p in ((App)App.Current).fileStorage.df.polls)
            {
                if (p.guid == g)
                {
                    this.p = p;
                }
            }

            questionName.Text = this.p.pollName;
            questionText.Text = this.p.questionText+ "\nCreated by: " + this.p.creator.name;

            foreach (Choice c in this.p.choices)
            {
                choicesList.Items.Add(c);
            }

            
        }

        private void Vote_Click(object sender, RoutedEventArgs e)
        {
            if (choicesList.SelectedIndex == -1)
            {
                DisplayMessage("You have to select a choice!", "Error");
                return;
            }
            /*if (p.creator.guid == (App.Current as App).fileStorage.df.login.guid)
            {
                DisplayMessage("You cannot vote on your poll!", "Error");
                NavigationService.GoBack();
                return;
            }*/ // Commented out since maybe you can vote on your own poll 
            foreach (Choice c in p.choices)
            {
                foreach (User u in c.voters)
                {
                    if (u.guid == (App.Current as App).fileStorage.df.login.guid)
                    {
                        DisplayMessage("You cannot if you have already voted before.", "Error");
                        NavigationService.GoBack();
                        return;
                    }
                }
            }


            PollAppMessage m = new PollAppMessage();
            Vote v = new Vote(((App)App.Current).fileStorage.df.login, this.p, (Choice)choicesList.SelectedItem);

            foreach (Choice c in p.choices)
            {
                if (c.guid == v.choiceGuid)
                {
                    c.votes++;
                    c.voters.Add(v.voter);
                    break;
                }
            }

            m.sender = ((App)App.Current).fileStorage.df.login;
            m.type = PollAppMessageType.newVote;
            m.data = v.getBytes();
            v.setBytes(m.data);
            ((App)App.Current).RelayContext.SendPollAppMessage(m);
            NavigationService.GoBack();
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
